import * as monaco from 'monaco-editor'
function createMonacoEditor(f) {
    const el =document.getElementById('editor')
    const e = document.createElement('div')
    e.style.width='100%'
    e.style.height='100%'
    el.append(e)
    const editor =  monaco.editor.create(e, {
       theme: "vs",
       language: "javascript",
       value: "/** powered by yudi **/",
       minimap:{
        enabled:false
       }
    })
    el.removeChild(e) // 解绑
    editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S,f);    
    return {el:e,editor}
}
const editors = new Array()
const  geteditor = function(f){
    if(editors.length==0){
    return createMonacoEditor(f)
    }else{
        const obj =  editors.pop()
        obj.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS,f);
        return obj
    }
}
const  setLanguage = function(obj,name){
    monaco.editor.setModelLanguage(obj.editor.getModel(),name.toLowerCase())
}
const pusheditor = function(obj){
    //this.obj.editor.setValue("/** powered by yudi **/")
  obj.editor.getModel().setValue("/** powered by yudi **/")
    if(obj.el.parentElement!=null){
        obj.el.parentElement.removeChild(obj.el)
    }
    editors.push(obj)
}
export {geteditor,pusheditor,setLanguage}